package com.action;

import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bean.ResponseBean;
import com.pojo.Major;
import com.pojo.Majortype;
import com.service.MajorService;
import com.service.MajortypeService;
import com.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @desc 专业群相关接口
 */
@RestController
@RequestMapping("/majortype")
public class MajortypeAction {
    @Autowired
    private MajortypeService majortypeService;

    @Value("${upload.dir}")
    private String upload ;

    @Value("${upload.tmp.dir}")
    private String tmp ;

    @Autowired
    private MajorService majorService ;

    /**
     *分页查询
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/types")
    public ResponseBean types(
            @RequestParam(defaultValue = "1")Integer page,
            @RequestParam(defaultValue = "10")Integer limit,
            @RequestParam(required = false)String key){
        if(StringUtil.isNull(key)){
            key = null ;
        }
        IPage iPage = majortypeService.types(page,limit,key);
        return new ResponseBean(0,iPage.getTotal(),"ok",iPage.getRecords());
    }
    /**
     * 查询所有专业群
     */
    @RequestMapping("/getTypeAll")
    public ResponseBean getTypeAll(){
        List<Majortype> iPage = majortypeService.list();
        return new ResponseBean(0,"ok",iPage);
    }

    /**
     * 添加专业群
     * @param majortype
     * @return
     */
    @RequestMapping("/addMajortype")
    public ResponseBean addMajortype(@RequestBody Majortype majortype){
        if(StringUtil.isNull(majortype.getMtimg())){
            return ResponseBean.error("请先上传图片");
        }
        if(StringUtil.isNull(majortype.getMtname())){
            return ResponseBean.error("请填写专业群名称");
        }
        Majortype m = majortypeService.getOne(new QueryWrapper<Majortype>().eq("mtname" , majortype.getMtname()).last("limit 1")) ;
        if(m != null) {
            return ResponseBean.error("名字已经存在") ;
        }
        boolean res = majortypeService.save(majortype);
        FileUtil.copy(tmp + majortype.getMtimg(), upload,true);
        return res ? new ResponseBean(200 ,"成功","") : ResponseBean.error("新增失败");
    }
    /**
     * 根据id查询
     * @return
     */
    @RequestMapping("/getMajortypeById")
    public ResponseBean getMajortypeById(String mtid){
        Majortype majortype = majortypeService.getById(mtid);
        if (majortype != null){
            return new ResponseBean(200,"查询成功",majortype);
        }
        return ResponseBean.error("查询失败");
    }

    /**
     * 修改专业群
     * @param majortype
     * @return
     */
    @RequestMapping("/updMajortype")
    public ResponseBean updMajortype(@RequestBody Majortype majortype){
        if(StringUtil.isNull(majortype.getMtid())){
            return ResponseBean.error("专业群id不能为空");
        }
        if(StringUtil.isNull(majortype.getMtname())){
            majortype.setMtname(null);
        }
        if(StringUtil.isNull(majortype.getMtimg())){
            majortype.setMtimg(null);
        }
        Majortype majorTwo = majortypeService.getById(majortype.getMtid());
        if(majortypeService.updateById(majortype)){
            if (!majortype.getMtimg().equals(majorTwo.getMtimg())){
                FileUtil.copy(tmp + majortype.getMtimg(), upload, true) ;
            }
            return ResponseBean.ok("操作成功");
        }
        return ResponseBean.error("操作失败");
    }

    /**
     * 根据id删除专业群
     * @param mtid
     * @return
     */
    @RequestMapping("/deleteMajortype")
    public ResponseBean deleteMajortype(String mtid){

        if(majortypeService.removeById(mtid)){
            majorService.remove(new QueryWrapper<Major>().eq("mtid" , mtid)) ;
            return new ResponseBean(0,1,"删除成功");
        }
        return new ResponseBean(1,1,"删除失败");
    }
}
